package com.elong.businesstravel.modules.home.hotel.hotelpayment;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Selection;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.support.jhf.debug.DebugLog;
import com.android.support.jhf.edittextclear.EditTextClearView;
import com.android.support.jhf.handlerui.HandlerPostUI;
import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.network.NetworkClient;
import com.android.support.jhf.network.loopj.android.http.ResponseHandlerInterface;
import com.android.support.jhf.utils.CheckIDCard;
import com.android.support.jhf.utils.DateUtils;
import com.android.support.jhf.utils.ToolUtils;
import com.elong.businesstravel.R;
import com.elong.businesstravel.base.baseactivity.BaseActivity;
import com.elong.businesstravel.base.dialog.CustomAlertDialog;
import com.elong.businesstravel.base.utils.SaveUserOperate;
import com.elong.businesstravel.base.view.TitleNavBarView;
import com.elong.businesstravel.entitys.Bank;
import com.elong.businesstravel.entitys.HotelDocs;
import com.elong.businesstravel.entitys.HotelPrice.GuaranteeRuleInfo;
import com.elong.businesstravel.entitys.HotelPrice.PrepayRuleInfo;
import com.elong.businesstravel.entitys.HotelPrice.RatePlanInfo;
import com.elong.businesstravel.modules.home.hotel.dialog.CreditCardValidityDialog;
import com.elong.businesstravel.modules.orders.OrdersActivity;
import com.elong.businesstravel.protocol.order.CreateOrderRequest;
import com.elong.businesstravel.protocol.order.CreateOrderResponse;
import com.umeng.analytics.MobclickAgent;

/**
 * 信用卡担保,和信用卡预付
 * 
 * @author lenovo
 * 
 */
public class CreditGuaranteesActivity extends BaseActivity {

	private RatePlanInfo mRatePlanInfo;
	private HotelDocs mHotelDocs;
	private String mTotalprice;
	private String mBouns;
	private String mTotalbouns;
	private String mPhoneString;
	private ArrayList<String> mLiveNameArrayList;
	private String mCurrentRoomAmount;
	private String mLatestarrival;
	private String mShowLatestarrival;
	private boolean mIsTimeGuarantee = false;
	private boolean mIsAmountGuarantee = false;
	private String mTitleString;
	private String mTipsString;
	/** 红包id */
	private String ticketid = "0";
	/** 红包价钱 */
	private String ticketcash = "0";

	// 发票信息
	private String needinvoice = "";
	private String invoicetitle = "";
	private String invoicetype = "";
	private String province = "";
	private String city = "";
	private String area = "";
	private String invoiceaddress = "";
	private String invoicename = "";
	private String invoicemobile = "";
	private String userscore = "";

	private TextView mBankTextView;
	private ViewGroup mBankLayout;

	private EditTextClearView mCardNumEditTextClearView;

	private ViewGroup mDateLayout;
	private TextView mDateTextView;

	private EditTextClearView mCardNameEditTextClearView;

	private EditTextClearView mIdCardEditTextClearView;

	private ViewGroup mCvv2Layout;
	private View mCvv2Divider;
	private EditTextClearView mCvv2EditTextClearView;

	private Bundle mSavedInstanceState;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mSavedInstanceState = savedInstanceState;
		setContentView(R.layout.activity_credit_guarantees);

		init();

	}

	@Override
	protected void init() {
		if (null == mSavedInstanceState) {
			mTitleString = getIntent().getStringExtra("title");
			mTipsString = getIntent().getStringExtra("tips");

			mIsTimeGuarantee = getIntent().getBooleanExtra("IsTimeGuarantee",
					false);
			mIsAmountGuarantee = getIntent().getBooleanExtra(
					"IsAmountGuarantee", false);
			mShowLatestarrival = getIntent()
					.getStringExtra("showLatestarrival");
			mRatePlanInfo = (RatePlanInfo) getIntent().getSerializableExtra(
					"RatePlanInfo");
			mHotelDocs = (HotelDocs) getIntent().getSerializableExtra(
					"HotelDocs");
			mTotalprice = getIntent().getStringExtra("totalprice");
			mBouns = getIntent().getStringExtra("bouns");
			mTotalbouns = getIntent().getStringExtra("totalbouns");
			mPhoneString = getIntent().getStringExtra("phoneString");
			mLiveNameArrayList = getIntent().getStringArrayListExtra(
					"liveNameArrayList");
			mCurrentRoomAmount = getIntent().getStringExtra(
					"currentRoomAmoount");
			mLatestarrival = getIntent().getStringExtra("latestarrival");

			needinvoice = getIntent().getStringExtra("needinvoice");
			if ("0".equals(needinvoice)) {
				// 不需要发票
			} else if ("1".equals(needinvoice)) {
				// 需要发票

				needinvoice = getIntent().getStringExtra("needinvoice");
				invoicetitle = getIntent().getStringExtra("invoicetitle");
				invoicetype = getIntent().getStringExtra("invoicetype");
				invoiceaddress = getIntent().getStringExtra("invoiceaddress");
				invoicename = getIntent().getStringExtra("invoicename");
				invoicemobile = getIntent().getStringExtra("invoicemobile");
			}
			userscore = getIntent().getStringExtra("userscore");
			ticketid = getIntent().getStringExtra("ticketid");
			ticketcash = getIntent().getStringExtra("ticketcash");
		} else {

			mRatePlanInfo = (RatePlanInfo) mSavedInstanceState
					.getSerializable("rateplaninfo");
			mHotelDocs = (HotelDocs) mSavedInstanceState
					.getSerializable("hoteldocs");
			mTotalprice = mSavedInstanceState.getString("totalprice");
			mBouns = mSavedInstanceState.getString("bouns");
			mTotalbouns = mSavedInstanceState.getString("totalbouns");
			mPhoneString = mSavedInstanceState.getString("phonestring");
			mLiveNameArrayList = mSavedInstanceState
					.getStringArrayList("livenamearraylist");
			mCurrentRoomAmount = mSavedInstanceState
					.getString("currentroomamount");
			mLatestarrival = mSavedInstanceState.getString("latestarrival");
			mShowLatestarrival = mSavedInstanceState
					.getString("showlatestarrival");
			mIsTimeGuarantee = mSavedInstanceState
					.getBoolean("istimegrarantee");
			mIsAmountGuarantee = mSavedInstanceState
					.getBoolean("isamountgrarantee");
			mTitleString = mSavedInstanceState.getString("titlestring");
			mTipsString = mSavedInstanceState.getString("tipsstring");
			ticketid = mSavedInstanceState.getString("ticketid");
			ticketcash = mSavedInstanceState.getString("ticketcash");
			// 发票信息
			needinvoice = mSavedInstanceState.getString("needinvoice");
			invoicetitle = mSavedInstanceState.getString("invoicetitle");
			invoicetype = mSavedInstanceState.getString("invoicetype");
			province = mSavedInstanceState.getString("province");
			city = mSavedInstanceState.getString("city");
			area = mSavedInstanceState.getString("area");
			invoiceaddress = mSavedInstanceState.getString("invoiceaddress");
			invoicename = mSavedInstanceState.getString("invoicename");
			invoicemobile = mSavedInstanceState.getString("invoicemobile");
			userscore = mSavedInstanceState.getString("userscore");
		}

		TextView guaranteesConditionTextView = (TextView) findViewById(R.id.guaranteesConditionTextView);
		if (!"".equals(mRatePlanInfo.GuaranteeRuleIds)
				&& mRatePlanInfo.GuaranteeRules.size() > 0) {
			GuaranteeRuleInfo guaranteeRuleInfo = mRatePlanInfo.GuaranteeRules
					.get(0);
			guaranteesConditionTextView.setVisibility(View.VISIBLE);
			guaranteesConditionTextView.setText(guaranteeRuleInfo.Description);
		} else if (!"".equals(mRatePlanInfo.PrepayRuleIds)
				&& mRatePlanInfo.PrepayRules.size() > 0) {
			PrepayRuleInfo prepayRuleInfo = mRatePlanInfo.PrepayRules.get(0);
			guaranteesConditionTextView.setText(prepayRuleInfo.Description);
			guaranteesConditionTextView.setVisibility(View.VISIBLE);
		} else {
			guaranteesConditionTextView.setVisibility(View.GONE);
		}

		TextView guaranteesMoneyTextView = (TextView) findViewById(R.id.guaranteesMoneyTextView);
		setTotalPriceFloatView(guaranteesMoneyTextView);

		TextView tipsTextView = (TextView) findViewById(R.id.tipsTextView);
		tipsTextView.setText(mTipsString);

		mCvv2Layout = (ViewGroup) findViewById(R.id.cvv2Layout);
		mCvv2Divider = findViewById(R.id.cvv2Divider);
		mCvv2Divider.setVisibility(View.GONE);
		mCvv2Layout.setVisibility(View.GONE);
		mCvv2EditTextClearView = (EditTextClearView) findViewById(R.id.cvv2EditTextClearView);
		mCvv2EditTextClearView.getEditText().setGravity(
				Gravity.CENTER_VERTICAL | Gravity.LEFT);
		mCvv2EditTextClearView.getEditText().setFilters(
				new InputFilter[] { new InputFilter.LengthFilter(3) });
		mCvv2EditTextClearView.getEditText().setInputType(
				InputType.TYPE_CLASS_NUMBER);
		mCvv2EditTextClearView.getEditText().setText(
				SaveUserOperate.getCvv2(mContext));

		mBankTextView = (TextView) findViewById(R.id.bankTextView);
		mBankLayout = (ViewGroup) findViewById(R.id.bankLayout);

		mCardNumEditTextClearView = (EditTextClearView) findViewById(R.id.cardNumEditTextClearView);
		mCardNumEditTextClearView.getEditText().setGravity(
				Gravity.CENTER_VERTICAL | Gravity.LEFT);
		mCardNumEditTextClearView.getEditText().setInputType(
				InputType.TYPE_CLASS_NUMBER);
		mCardNumEditTextClearView.getEditText().setText(
				SaveUserOperate.getCreditCardNumber(mContext));
		mCardNumEditTextClearView.getEditText().setFilters(
				new InputFilter[] { new InputFilter.LengthFilter(24) });
		bankCardNumAddSpace(mCardNumEditTextClearView.getEditText());

		mDateLayout = (ViewGroup) findViewById(R.id.dateLayout);
		mDateTextView = (TextView) findViewById(R.id.dateTextView);

		mCardNameEditTextClearView = (EditTextClearView) findViewById(R.id.cardNameEditTextClearView);
		mCardNameEditTextClearView.getEditText().setGravity(
				Gravity.CENTER_VERTICAL | Gravity.LEFT);
		mCardNameEditTextClearView.getEditText().setText("");
		String cardNameString = SaveUserOperate.getCardholder(mContext);
		if (TextUtils.isEmpty(cardNameString)) {
			cardNameString = SaveUserOperate.getLiveName(mContext);
		}
		mCardNameEditTextClearView.getEditText().setText(cardNameString);

		mIdCardEditTextClearView = (EditTextClearView) findViewById(R.id.idCardEditTextClearView);
		mIdCardEditTextClearView.getEditText().setGravity(
				Gravity.CENTER_VERTICAL | Gravity.LEFT);
		// mIdCardEditTextClearView.getEditText().setInputType(
		// InputType.TYPE_CLASS_NUMBER);
		mIdCardEditTextClearView.getEditText().setText(
				SaveUserOperate.getIdCard(mContext));

		setTitleNavBar();

		setListener();
	}

	private void setTotalPriceFloatView(TextView textView) {
		float totalPrice = 0.0F;
		int tempticketcash = 0;
		try {
			totalPrice = Float.parseFloat(mTotalprice);
			float tempUserscore = 0.0F;
			if (!TextUtils.isEmpty(userscore)) {
				tempUserscore = Float.parseFloat(userscore);
			}
			if (!TextUtils.isEmpty(this.ticketcash)) {
				tempticketcash = ToolUtils.intByString(ticketcash);
			}
			totalPrice = totalPrice - tempUserscore - tempticketcash;
			BigDecimal bd = new BigDecimal(totalPrice);
			bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);

			String moneyString = "";
			if (bd.floatValue() % 1.0 == 0) {
				moneyString = "￥" + (int) (bd.floatValue());
			} else {
				moneyString = "￥" + bd.floatValue();
			}

			SpannableStringBuilder moneySpannable = new SpannableStringBuilder(
					moneyString);
			moneySpannable.setSpan(new ForegroundColorSpan(mContext
					.getResources().getColor(R.color.list_gray_text_color)), 0,
					1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			float moneySize = mContext.getResources().getDimension(
					R.dimen.list_text_small_size);
			moneySpannable.setSpan(new AbsoluteSizeSpan((int) moneySize), 0, 1,
					Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			textView.setText(moneySpannable);

		} catch (Exception e) {
		}

	}

	@Override
	protected void setListener() {

		Button submitButton = (Button) findViewById(R.id.submitButton);
		submitButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				createOrders();
			}
		});

		mBankLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, BankListActivity.class);
				startActivityForResult(intent, 0);
				overridePendingTransition(
						R.anim.activity_bottom_top_enter_anim_translate,
						R.anim.activity_alpha_fixed);
			}
		});

		mDateLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showCreditCardValidity();
			}
		});
	}

	/**
	 * 银行卡四位加空格
	 * 
	 * @param mEditText
	 */
	protected void bankCardNumAddSpace(final EditText mEditText) {
		mEditText.addTextChangedListener(new TextWatcher() {
			int beforeTextLength = 0;
			int onTextLength = 0;
			boolean isChanged = false;

			int location = 0;// 记录光标的位置
			private char[] tempChar;
			private StringBuffer buffer = new StringBuffer();
			int konggeNumberB = 0;

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				beforeTextLength = s.length();
				if (buffer.length() > 0) {
					buffer.delete(0, buffer.length());
				}
				konggeNumberB = 0;
				for (int i = 0; i < s.length(); i++) {
					if (s.charAt(i) == ' ') {
						konggeNumberB++;
					}
				}
			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				onTextLength = s.length();
				buffer.append(s.toString());
				if (onTextLength == beforeTextLength || onTextLength <= 3
						|| isChanged) {
					isChanged = false;
					return;
				}
				isChanged = true;
			}

			@Override
			public void afterTextChanged(Editable s) {
				if (isChanged) {
					location = mEditText.getSelectionEnd();
					int index = 0;
					while (index < buffer.length()) {
						if (buffer.charAt(index) == ' ') {
							buffer.deleteCharAt(index);
						} else {
							index++;
						}
					}

					index = 0;
					int konggeNumberC = 0;
					while (index < buffer.length()) {
						if ((index == 4 || index == 9 || index == 14 || index == 19)) {
							buffer.insert(index, ' ');
							konggeNumberC++;
						}
						index++;
					}

					if (konggeNumberC > konggeNumberB) {
						location += (konggeNumberC - konggeNumberB);
					}

					tempChar = new char[buffer.length()];
					buffer.getChars(0, buffer.length(), tempChar, 0);
					String str = buffer.toString();
					if (location > str.length()) {
						location = str.length();
					} else if (location < 0) {
						location = 0;
					}

					mEditText.setText(str);
					Editable etable = mEditText.getText();
					Selection.setSelection(etable, location);
					isChanged = false;
				}
			}
		});
	}

	private void showCreditCardValidity() {
		String dateString = mDateTextView.getText().toString();
		String yearString = "";
		String monthString = "";
		if (!TextUtils.isEmpty(dateString)) {
			yearString = DateUtils.dateFormat(dateString, "yyyy年MM月", "yyyy年");
			monthString = DateUtils.dateFormat(dateString, "yyyy年MM月", "M月");
		}
		CreditCardValidityDialog creditCardValidityDialog = new CreditCardValidityDialog(
				mContext, com.android.support.jhf.R.style.DialogFromDownToUp,
				yearString, monthString);
		creditCardValidityDialog.setOnItemListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				String yearMonth = (String) arg1.getTag();
				mDateTextView.setText(yearMonth);
			}
		});
		creditCardValidityDialog.show();

	}

	@Override
	protected void setTitleNavBar() {
		TitleNavBarView titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage(mTitleString);
		titleNavBarView.setLeftDrawableImage(R.drawable.btn_title_back);
		titleNavBarView.setLeftButtonImageString(0, "返回", R.color.text_blue);
		titleNavBarView.setLeftButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				quit();
			}
		});
		titleNavBarView.setRightButtonVisibility(View.INVISIBLE);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case 0: {
			// 发卡银行返回
			switch (resultCode) {
			case RESULT_OK: {
				Bank bank = (Bank) data.getSerializableExtra("bank");
				mBankTextView.setTag(bank);
				mBankTextView.setText(bank.toString());
				if ("1".equals(bank.isCVV)) {
					mCvv2Layout.setVisibility(View.VISIBLE);
					mCvv2Divider.setVisibility(View.VISIBLE);

				} else if ("0".equals(bank.isCVV)) {
					mCvv2Divider.setVisibility(View.GONE);
					mCvv2Layout.setVisibility(View.GONE);
				}
				break;
			}
			case RESULT_CANCELED: {
				break;
			}
			default:
				break;
			}
			break;
		}
		case 1: {
			// 信用卡有效期返回
			switch (resultCode) {
			case RESULT_OK: {
				String dateString = data.getStringExtra("yearMonth");
				dateString = DateUtils.dateFormat(dateString, "yyyy年MM月",
						"yyyy年MM月");
				mDateTextView.setText(dateString);
				break;
			}
			case RESULT_CANCELED: {
				break;
			}
			default:
				break;
			}
			break;
		}
		default:
			break;
		}
	}

	private void createOrders() {
		StringBuilder stringBuilder = new StringBuilder();
		if (!checkSubmitInfo(stringBuilder)) {
			HandlerToastUI
					.getHandlerToastUI(mContext, stringBuilder.toString());
			return;
		}

		// 保存信息
		SaveUserOperate.setCardholder(mContext, mCardNameEditTextClearView
				.getEditText().getText().toString());
		SaveUserOperate.setIdCard(mContext, mIdCardEditTextClearView
				.getEditText().getText().toString());
		SaveUserOperate.setCreditCardNumber(mContext, mCardNumEditTextClearView
				.getEditText().getText().toString());
		SaveUserOperate.setCvv2(mContext, mCvv2EditTextClearView.getEditText()
				.getText().toString());

		String starttime = "";
		String endtime = "";
		if (!"".equals(mRatePlanInfo.GuaranteeRuleIds)
				&& mRatePlanInfo.GuaranteeRules.size() > 0) {
			GuaranteeRuleInfo info = mRatePlanInfo.GuaranteeRules.get(0);
			starttime = info.StartTime;
			endtime = info.EndTime;
		}
		String startdate = mApplication.mHotelSearchCondition.getStartDate();
		String enddate = mApplication.mHotelSearchCondition.getEndDate();
		String earlyarrival = "";
		StringBuilder customer = new StringBuilder();
		List<String> liveNameList = mLiveNameArrayList;
		for (int i = 0; i < liveNameList.size(); i++) {
			customer.append(liveNameList.get(i)).append(",");
		}
		customer.delete(customer.length() - 1, customer.length());
		String paymentType = ""; // Prepay","Selfpay"
		if (mRatePlanInfo.PaymentType.equalsIgnoreCase("PrePay")) {
			paymentType = "Prepay";
		} else if (mRatePlanInfo.PaymentType.equalsIgnoreCase("SelfPay")) {
			paymentType = "Selfpay";
		}

		// 担保
		String needcreditcard = "1";
		String crednum = mCardNumEditTextClearView.getEditText().getText()
				.toString().replace(" ", "");
		String cvv = mCvv2EditTextClearView.getEditText().getText().toString();
		String expyear = DateUtils.dateFormat(mDateTextView.getText()
				.toString(), "yyyy年MM月", "yyyy");
		String expmonth = DateUtils.dateFormat(mDateTextView.getText()
				.toString(), "yyyy年MM月", "MM");
		;
		String holdername = mCardNameEditTextClearView.getEditText().getText()
				.toString();
		String idtype = "0";
		String idno = mIdCardEditTextClearView.getEditText().getText()
				.toString();
		String bouns = Integer.parseInt(mRatePlanInfo.Bonus) + "";
		String paymentmothd = "";
		String paymentmothds = "";

		String guaranteeType = "";
		boolean guarantee = false;
		if ((!TextUtils.isEmpty(mRatePlanInfo.GuaranteeRuleIds) && mRatePlanInfo.GuaranteeRules
				.size() > 0)) {
			GuaranteeRuleInfo guaranteeRuleInfo = mRatePlanInfo.GuaranteeRules
					.get(0);
			int amount = 100;
			int currentAmount = 0;
			try {
				amount = ToolUtils.intByString(guaranteeRuleInfo.Amount);
				currentAmount = ToolUtils.intByString(mCurrentRoomAmount);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (!mIsAmountGuarantee && !mIsTimeGuarantee) {
				guarantee = true;
			} else {
				// if (mLatestarrival.contains("担保")
				if (mShowLatestarrival.contains("担保")
						|| (mIsAmountGuarantee && currentAmount >= amount)) {
					guarantee = true;
				}
			}
		}
		if (guarantee) {
			guaranteeType = (!mIsAmountGuarantee && !mIsTimeGuarantee) ? "2"
					: "1";
		} else {
			// 无担保
			guaranteeType = "0";
		}

		// 0无担保 1 T点之后担保 2全天担保
		final String tempGuaranteeType = guaranteeType;
		// Selfpay-前台现付、Prepay-预付
		final String tempPaymentType = paymentType;
		NetworkClient.getNetworkClient().PostRequest(
				new CreateOrderRequest(mContext, mHotelDocs.hotelId,
						mRatePlanInfo.RoomTypeId, mRatePlanInfo.RateId,
						mHotelDocs.hotelName, mRatePlanInfo.RoomName,
						mRatePlanInfo.RateName, starttime, endtime,
						mCurrentRoomAmount + "", mRatePlanInfo.CustomerType,
						paymentType, mTotalprice + "", startdate, enddate,
						earlyarrival, mLatestarrival, customer.toString(),
						mPhoneString, needcreditcard, crednum, cvv, expyear,
						expmonth, holdername, idtype, idno, needinvoice,
						invoicetitle, invoicetype, province, city, area,
						invoiceaddress, invoicename, invoicemobile, userscore,
						bouns, mTotalbouns, paymentmothd, paymentmothds,
						guaranteeType, ticketid, ticketcash),
				new CreateOrderResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						if (1 == status) {
							HandlerPostUI.getHandlerPostUI(new Runnable() {

								@Override
								public void run() {
									if (tempPaymentType
											.equalsIgnoreCase("PrePay")) {
										// 信用卡预付
										MobclickAgent.onEvent(mContext,
												"Credit_PrePay");
									} else if (tempPaymentType
											.equalsIgnoreCase("SelfPay")) {
										// 现付
										if ("0".equals(tempGuaranteeType)) {
											// 0无担保
											MobclickAgent.onEvent(mContext,
													"SelfPay_Unsecured");
										} else if ("1"
												.equals(tempGuaranteeType)) {
											// 1 T点之后担保
											MobclickAgent.onEvent(mContext,
													"SelfPay_Guarantee_Order");
										} else if ("2"
												.equals(tempGuaranteeType)) {
											// 2全天担保
											MobclickAgent.onEvent(mContext,
													"SelfPay_Guarantee_Order");
										}
									}

									showDialog();
								}
							});
						} else if (0 == status) {
							HandlerToastUI.getHandlerToastUI(mContext,
									resultTextString);
						}
					}
				});

	}

	private void showDialog() {

		CustomAlertDialog customAlertDialog = new CustomAlertDialog(mContext,
				R.style.CustomAlertDialog);
		customAlertDialog.setMessage("提交成功");
		customAlertDialog.setLeftButton("查看订单", new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(mContext, OrdersActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
			}
		});
		customAlertDialog.setCancelable(false);
		customAlertDialog.setCanceledOnTouchOutside(false);
		customAlertDialog.setRightButtonGone();
		customAlertDialog.show();
	}

	private boolean checkSubmitInfo(StringBuilder stringBuilder) {
		stringBuilder.delete(0, stringBuilder.length());
		if (TextUtils.isEmpty(mBankTextView.getText().toString())) {
			stringBuilder.append("请选择发卡银行");
			return false;
		} else if (TextUtils.isEmpty(mCardNumEditTextClearView.getEditText()
				.getText().toString())) {
			stringBuilder.append("请输入卡号");
			return false;
		} else if (TextUtils.isEmpty(mDateTextView.getText().toString())) {
			stringBuilder.append("请选择有效期");
			return false;
		} else if (View.VISIBLE == mCvv2Layout.getVisibility()
				&& TextUtils.isEmpty(mCvv2EditTextClearView.getEditText()
						.getText().toString())) {
			stringBuilder.append("请输入卡背末三位数字");
			return false;
		} else if (View.VISIBLE == mCvv2Layout.getVisibility()
				&& 3 != mCvv2EditTextClearView.getEditText().getText()
						.toString().length()) {
			stringBuilder.append("请输入卡背末三位数字");
			return false;
		} else if (TextUtils.isEmpty(mCardNameEditTextClearView.getEditText()
				.getText().toString())) {
			stringBuilder.append("请输入持卡人真实姓名");
			return false;
		} else if (TextUtils.isEmpty(mIdCardEditTextClearView.getEditText()
				.getText().toString())) {
			stringBuilder.append("请输入身份证号码");
			return false;
		} else if (!CheckIDCard.IDCardValidate(mIdCardEditTextClearView
				.getEditText().getText().toString().toLowerCase(),
				stringBuilder)) {
			return false;
		}
		return true;
	}

	private void quit() {
		// 保存信息
		SaveUserOperate.setCardholder(mContext, mCardNameEditTextClearView
				.getEditText().getText().toString());
		SaveUserOperate.setIdCard(mContext, mIdCardEditTextClearView
				.getEditText().getText().toString());
		SaveUserOperate.setCreditCardNumber(mContext, mCardNumEditTextClearView
				.getEditText().getText().toString());
		SaveUserOperate.setCvv2(mContext, mCvv2EditTextClearView.getEditText()
				.getText().toString());
		finish();

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			quit();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {

		outState.putSerializable("rateplaninfo", mRatePlanInfo);
		outState.putSerializable("hoteldocs", mHotelDocs);
		outState.putString("totalprice", mTotalprice);
		outState.putString("bouns", mBouns);
		outState.putString("totalbouns", mTotalbouns);
		outState.putString("phonestring", mPhoneString);
		outState.putStringArrayList("livenamearraylist", mLiveNameArrayList);
		outState.putString("currentroomamount", mCurrentRoomAmount);
		outState.putString("latestarrival", mLatestarrival);
		outState.putString("showlatestarrival", mShowLatestarrival);
		outState.putBoolean("istimegrarantee", mIsTimeGuarantee);
		outState.putBoolean("isamountgrarantee", mIsAmountGuarantee);
		outState.putString("titlestring", mTitleString);
		outState.putString("tipsstring", mTipsString);
		outState.putString("ticketid", ticketid);
		outState.putString("ticketcash", ticketcash);
		// 发票信息
		outState.putString("needinvoice", needinvoice);
		outState.putString("invoicetitle", invoicetitle);
		outState.putString("invoicetype", invoicetype);
		outState.putString("province", province);
		outState.putString("city", city);
		outState.putString("area", area);
		outState.putString("invoiceaddress", invoiceaddress);
		outState.putString("invoicename", invoicename);
		outState.putString("invoicemobile", invoicemobile);
		outState.putString("userscore", userscore);

		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		if (null != savedInstanceState) {
			mRatePlanInfo = (RatePlanInfo) savedInstanceState
					.getSerializable("rateplaninfo");
			mHotelDocs = (HotelDocs) savedInstanceState
					.getSerializable("hoteldocs");
			mTotalprice = savedInstanceState.getString("totalprice");
			mBouns = savedInstanceState.getString("bouns");
			mTotalbouns = savedInstanceState.getString("totalbouns");
			mPhoneString = savedInstanceState.getString("phonestring");
			mLiveNameArrayList = savedInstanceState
					.getStringArrayList("livenamearraylist");
			mCurrentRoomAmount = savedInstanceState
					.getString("currentroomamount");
			mLatestarrival = savedInstanceState.getString("latestarrival");
			mShowLatestarrival = savedInstanceState
					.getString("showlatestarrival");
			mIsTimeGuarantee = savedInstanceState.getBoolean("istimegrarantee");
			mIsAmountGuarantee = savedInstanceState
					.getBoolean("isamountgrarantee");
			mTitleString = savedInstanceState.getString("titlestring");
			mTipsString = savedInstanceState.getString("tipsstring");
			ticketid = savedInstanceState.getString("ticketid");
			ticketcash = savedInstanceState.getString("ticketcash");
			// 发票信息
			needinvoice = savedInstanceState.getString("needinvoice");
			invoicetitle = savedInstanceState.getString("invoicetitle");
			invoicetype = savedInstanceState.getString("invoicetype");
			province = savedInstanceState.getString("province");
			city = savedInstanceState.getString("city");
			area = savedInstanceState.getString("area");
			invoiceaddress = savedInstanceState.getString("invoiceaddress");
			invoicename = savedInstanceState.getString("invoicename");
			invoicemobile = savedInstanceState.getString("invoicemobile");
			userscore = savedInstanceState.getString("userscore");

			mBankTextView.setText("");

			mCardNumEditTextClearView.getEditText().setText("");

			mDateTextView.setText("");

			mCardNameEditTextClearView.getEditText().setText("");

			mIdCardEditTextClearView.getEditText().setText("");

			mCvv2EditTextClearView.getEditText().setText("");
		}
	}

}
